
// rewamped moo getComputedStyle, extracted and refactored ViewCSS.js from Dean Edwards Base
// getComputedStyle should always return pixies and colors in rgb also in IE (as w3c does)
// by: Daniel Steigerwald (daniel.steigerwald.cz)
// + fix also for white
// + fix for backgroundPosition
// + fix for float
if (Browser.Engine.trident) {
	(function() {
		var _PIXEL = /^\d+(px)?$/i,
			_METRICS = /(width|height|top|bottom|left|right|fontSize)$/i,
			_COLOR = /^(color|backgroundColor)$/,
			_EDGE_COLOR_TEST_FOR_CRAPPY_IE = {
				'rgb(0, 0, 0)': { black: 1, '#000': 1, '#000000': 1 },
				// I had to add this white check addition to base2, probably from same reason as Dean had added black. In my bigass app, sometimes its returned rgb(255, 255, 255) instead of correct color. Other colors works well.
				'rgb(255, 255, 255)': { white: 1, '#fff': 1, '#ffffff': 1 }
			};

		function getPixelValue(el, value) {
			if (_PIXEL.test(value)) return parseInt(value);
			var styleLeft = el.style.left,
			runtimeStyleLeft = el.runtimeStyle.left;
			el.runtimeStyle.left = el.currentStyle.left;
			el.style.left = value || 0;
			value = el.style.pixelLeft;
			el.style.left = styleLeft;
			el.runtimeStyle.left = runtimeStyleLeft;
			return value;
		};

		function getColorValue(el, type) {
			var range, zoom = el.style.zoom;
			// Dean: elements need to have "layout" for this to work, Steida: So I add layout temporally
			if (!el.currentStyle || !el.currentStyle.hasLayout) el.style.zoom = 1;
			if (el.createTextRange) range = el.createTextRange();
			else {
				range = el.document.body.createTextRange();
				range.moveToElementText(el);
			}
			var color = range.queryCommandValue(type);
			el.style.zoom = zoom;
			return 'rgb({0}, {1}, {2})'.substitute([color & 0xff, (color & 0xff00) >> 8, (color & 0xff0000) >> 16]);
		};

		Element.implement({
			getComputedStyle: function(property) {
				property = property.camelCase();
				if (property == 'backgroundPosition') {
					return this.currentStyle[property + 'X'] + ' ' + this.currentStyle[property + 'Y'];
				}
				if (property == 'float') return this.currentStyle['styleFloat'];
				var value = this.currentStyle[property];
				if (_METRICS.test(property)) return getPixelValue(this, value) + 'px';
				if (_COLOR.test(property)) {
					var rgb = getColorValue(this, property == 'color' ? 'ForeColor' : 'BackColor'),
						test = _EDGE_COLOR_TEST_FOR_CRAPPY_IE[rgb];
					// todo: add named color conversion for value
					return (test && !test[value]) ? ('rgb(' + new Color(value) + ')') : rgb;
				}
				return value;
			}
		});

	})();
}
else {
	Element.implement({
		getComputedStyle: function(property) {
			var computed = this.getDocument().defaultView.getComputedStyle(this, null);
			return (computed) ? computed.getPropertyValue([property.hyphenate()]) : null;
		}
	});
}

